package com.huanzhidadi.ratelimiter.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping(value = "fastjson")
public class FastjsonController {

    @Autowired
    protected HttpServletResponse response;
    @Autowired
    protected HttpServletRequest request;

    private Integer count = 0;

    /**
     * {
     *   "aaa": "  123  ",
     *   "bbb": "null",
     *   "ccc": null
     * }
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/getJsonString", method = RequestMethod.POST)
    public String getJsonString(@RequestBody JSONObject jsonObject) {
        count++;
        log.info("count: " + count);
        log.info(jsonObject.getString("aaa"));
        jsonObject.put("count", count);
        String jsonStr = JSON.toJSONString(jsonObject, JSONWriter.Feature.WriteMapNullValue);
        return jsonStr;
    }


}
